Skip to content

Validate enforced sessions and document migration - #7364

Merged
FarhanAliRaza merged 124 commits into
mainfrom
codex/session-integration
Nov 30, 2026
Merged

FarhanAliRaza merged 124 commits into
mainfrom
codex/session-integration

Conversation

@masenf

@masenf masenf commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Exercise session ownership in real servers and browsers, migrate synthetic clients to server-issued credentials, and document deployment and rollout.

The backend-path app runs under enforcement across development/production and zero/one/two-level prefixes. It covers immediate hydration during held cookie exchange, shared/duplicate tabs, uploads, failed exchange recovery with retained or cancelled uploads, idle short-TTL renewal, backend restarts, and cross-site iframe persistence. EmbedPlugin coverage also runs under enforcement. Production restarts preserve their worker and compile-suppression settings.

A real Granian test verifies state events while cookie exchange is held and state continuity on reconnect. It uses rx.State and asserts it leaves no direct BaseState subclass behind for later integration apps. Benchmark reconnects reuse cookies and bound tokens, with issuance outside measured event latency.

The guide covers app-bound credentials, persistent/rotating signing keys, POSIX permissions and Windows ACLs, origin configuration, partitioned cookies, embedding, migration modes, SYSTEM/anonymous authority, idle renewal, retry/cancellation, and readiness during cookie replacement. Existing reverse-proxy/api_transformer facilities handle rate limiting.

Review order: #7361 → #7362 → #7363 → #7364. Merged with current main; #7360 is already merged. Companion: reflex-dev/reflex-enterprise#240.

Validation:

  • Full unit suite: 12,147 passed, 26 skipped; 80.15% coverage (72% required).
  • Final Chromium matrix: 70 passed across dev/prod/backend paths and embedding, including idle renewal, failed exchange recovery, cancellation, and six production/development restart configurations.
  • Isolated real Redis transport/context/upload suite: 390 passed.
  • Real Granian plus database import-order regressions: 15 passed.
  • All 13 frontend runtime/lifecycle wrappers pass, including cookie-replacement readiness and teardown regressions.
  • Ruff check/format and pyright reflex tests pass. Minimum-dependency checks pass for reflex, reflex-base, and reflex-components-core. Stub generation makes no tracked changes.
  • Full local pre-commit has six existing macOS-only Pyright errors in unchanged reflex-bench/reflex-workflow CPU-affinity code. All other hooks pass; CI pre-commit passes on Linux.
  • The advisory playground-0.8.23 job fails on that older release's missing greenlet dependency. Current-head playground and size checks pass. Large CI matrices are still running after the final pushes; cancelled duplicate runs also temporarily report failed gate checks.

All substantive feedback is addressed. Review-thread replies explain the intentional connection-lifetime authority policy and declined hypothetical/style-only requests. Final adversarial review found no additional code blockers.

The guide now also covers: cookie renewal only at POST /_reflex/session, trust for a page on the backend host, the 403 exchange result, hosts where the browser stores no Secure cookie (plain HTTP), the shared-state link check, and that all ports of the backend host share the cookie.

The guide documents enforce as the default mode, how to change it with REFLEX_SESSION_TOKEN_MODE, and what an upgrading app can see.

Breaking: this pull request changes the REFLEX_SESSION_TOKEN_MODE default to enforce. State access for a client token that the session does not own raises SessionAuthorizationError, uploads for such a token return 403, and a tab that presents such a token gets a new token and fresh state. warn allows it with a warning and off disables the check. Unit tests and the docs example tests run in warn, and the in-process benchmarks in off, through fixtures with their own MonkeyPatch context, because they address state with plain tokens. The self-hosting page lists /_reflex/session with the routes that a reverse proxy must forward.

Related to ENG-12920, ENG-12921, ENG-9088.

@masenf
masenf added this pull request to stack #7365 September 30, 2026 00:41
@masenf masenf changed the title Exercise enforced sessions across servers and browsers and document migration Validate enforced sessions and document migration Sep 30, 2026
@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · This PR appears safe to merge; no actionable findings remain.RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] This PR appears safe to merge; no actionable findings remain.

Summary

This PR makes session ownership enforced by default, adds server and browser coverage, and documents deployment and migration.

  • State access now requires the session that owns the client token.
  • Deployment guides show how to keep browser sessions working across reloads.
  • Browser tests now exercise session ownership across real server setups.
  • Benchmark sockets now reconnect with server-issued session credentials.

Reviews (34) · Last reviewed commit: "Document the arguments of the session te..." · Reviewed by Greptile

@masenf
masenf force-pushed the codex/session-integration branch from 80666f1 to 62db86e Compare October 5, 2026 21:24
@masenf masenf added the documentation Improvements or additions to documentation label Oct 6, 2026
@linear-code

linear-code Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

ENG-12921

ENG-9088

ENG-12920

@masenf
masenf force-pushed the codex/session-integration branch from 62db86e to c561071 Compare October 8, 2026 17:55
@masenf
masenf force-pushed the codex/session-integration branch from c561071 to db8a6ab Compare October 8, 2026 18:24
@masenf
masenf force-pushed the codex/session-integration branch 2 times, most recently from 86fdb35 to 5932c7d Compare October 8, 2026 19:03
@masenf
masenf force-pushed the codex/session-integration branch from 5932c7d to 6b07618 Compare October 14, 2026 09:35
An event context without an authorized session read and parsed
REFLEX_SESSION_TOKEN_MODE on each creation (1.9 us). The parsed mode is
now cached by the raw environment value (0.36 us), so a change to the
environment still takes effect.
HTTP calls wait whenever the socket delivers a credential, on a first
connection and on a reconnection alike. The flag that exempted later
credentials was never cleared on disconnect, so an upload after a
reconnection with an expired cookie could start before the new cookie.
The benchmarks connect a namespace that has no server and address state
with a plain token. A connection now issues a session over the socket,
so test_on_event_router_data failed at setup. The fixture moves here
from the integration PR.
The harness restarts the backend in the same process on a new event
loop. The redis token manager holds an asyncio.Event that is bound to
the stopped loop, so each connection after the restart failed on the
redis shards. A real restart is a new process with a new manager.
# Conflicts:
#	packages/reflex-base/src/reflex_base/config.py
#	packages/reflex-base/src/reflex_base/session.py
#	tests/units/reflex_base/test_session.py
Base automatically changed from codex/session-frontend to main November 25, 2026 16:50
# Conflicts:
#	news/+browser-session-transport.feature.md
#	packages/reflex-base/news/+session-cookie-bootstrap.feature.md
The session survived the restart only because the generated key lives in
the test process. A real `reflex run` restart without REFLEX_SECRET_KEY
gets a new key and resets every session. Set a key, as a deployment does.
An upgraded deployment without REFLEX_SECRET_KEY resets every session on
restart, and replicas reject each other's sessions. A reverse proxy that
does not forward /_reflex/session leaves the browser without a cookie, so
each reload starts with fresh state. The upgrade guide, the breaking
change notes, the self-hosting guide and the container examples now say
to set the key and route the path before deploying.

warn and off turn the ownership protection off. The guide and the notes
no longer suggest warn as a migration step: they ask every app to keep
enforce and describe warn and off as escape hatches for a broken
deployment. The deprecation note that called warn the default is removed;
the breaking change note covers the new default.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 30 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread tests/units/test_testing.py Resolved
Comment thread docs/hosting/self-hosting.md Resolved
Comment thread docs/api-routes/sessions.md Resolved
Comment thread tests/units/reflex_base/test_session.py Resolved
Put the rule to keep enforce and the rule to share one secret key in
warning alerts, as other Reflex pages mark critical rules. Collect the
environment variables and rx.Config options in a settings table with
their defaults. Use one term for each value: session cookie, client token,
and exchange credential. Move the request-level detail of the exchange
and the renewal into a "How browser sessions work" section at the end,
and give clients that are not browsers their own section. No behaviour
is described differently.
Remove how the exchange credential, renewal timers, Web Locks and storage
keys work, the cookie attribute strings, and notes that belong in release
notes. Turn the remaining behaviour into a troubleshooting list keyed by
symptom. The guide goes from about 2,150 to 1,150 words.
The integration tests covered sessions that work. Add tests for the
sessions that end, are rejected, or cannot be set up:

- test_session_lifecycle: a fresh tab gets its cookie and keeps its state
  across a reload and a renewal. Another browser that presents its client
  token gets a new token and fresh state. Without its cookie the tab cannot
  upload, and a reload starts a new session with fresh state. At the
  maximum age the tab gets a new session and fresh state, and uploads work.
- test_session_server_code_needs_the_owning_session: a handler cannot change
  another session's state, and SessionToken.SYSTEM can.
- test_session_legacy_token_gets_fresh_state: a token from before sessions
  does not claim the state stored under it.
- test_session_exchange_failure: a lasting failure holds uploads for three
  attempts, a 403 is not retried, and in both cases events keep working
  while uploads are rejected.

BackendPathApp gets a handler that changes the counter of another client
token, with its own session or as SYSTEM.
While the cookie exchange keeps failing, the page also shows a connection toast, so the toast locator matched two elements and failed in strict mode.
REFLEX_SKIP_COMPILE is internal, so its environment name is __REFLEX_SKIP_COMPILE. The test set the public name, which changed nothing. It now sets the real name and checks that compile suppression starts off. Also update the rollout-mode test docstring to the enforce default.
… workers

Without the key, each worker process that is not started by reflex run generates its own key, so the workers of one backend reject each other's sessions.
@FarhanAliRaza
FarhanAliRaza merged commit 3558533 into main Nov 30, 2026
171 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the codex/session-integration branch November 30, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation on deck PRs lined up to review / merge next roadmap https://github.com/reflex-dev/reflex/issues/2727 security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants